package com.xrx.webtemplate.controller;

import com.xrx.webtemplate.common.ActionResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Created by xierongxian on 2017/4/23.
 */
@Controller
    @RequestMapping("login")
public class LoginController {

    /**
     * 登录get方法跳转至登录页面
     * */
    @GetMapping("loginView")
    public String Login(){
        return "login";
    }
    /**
     * 登录post方法 验证登录信息
     * */
    @PostMapping("login")
    @ResponseBody
    public ActionResult Login(@RequestParam("username") String username,@RequestParam("password") String password){
        ActionResult actionResult=new ActionResult();
        Subject subject= SecurityUtils.getSubject();
        actionResult.setCode(200);
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try{
            subject.login(token);// 登录
        }catch (UnknownAccountException unknownAccountException){
            actionResult.setCode(500);
            actionResult.setHint("用户名或密码错误！");
        }catch (DisabledAccountException disabledAccountException){
            actionResult.setCode(500);
            actionResult.setHint("账号锁定！");
        }
        return actionResult;


    }

}
